package com.example.bibuproject.service

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
import androidx.annotation.RequiresApi
import com.wink_172.activity.DownloadListActivity
import com.wink_172.library.AppManager
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.model.Constants.local_path
import com.wink_172.library.model.json.CustomEventBus
import com.wink_172.library.model.json.FileData
import com.wink_172.library.utils.FileUtil
import com.wink_172.library.utils.FileUtil.Companion.getFileNameWithSuffix
import com.wink_172.library.utils.HttpRequest.get_test_imge_listbb
import com.wink_172.library.utils.MMKVTool
import com.wink_172.model.Constants.down_loading
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.xutils.x
import java.io.*

/**
 * 日常事务处理服务及日志管理
 */
class BackgroundService : Service() {

    private var binder: ServiceBinder? = null

    open val TAG = "BackgroundService"


    lateinit open var handler: MyHandler


    inner class MyHandler : Handler() {
        override fun handleMessage(msg: Message) {
            handlerCallBack(msg)
        }
    }

    open fun handlerCallBack(msg: Message?) {
        when (msg!!.what) {
            com.wink_172.model.Constants.EventEnum.检查文件是否有更新.ordinal -> {

                if (down_loading == false) {
                    get_test_imge_listbb(object : ICallback {
                        override fun onSendEvent(event: Int, vararg objects: Any?) {
                            if (event == Constants.EventEnum.网络请求成功.ordinal) {
                                Log.e(TAG, "onSendEvent: ====>>获取列表2")
                                val cc_list = objects[0] as MutableList<FileData>

                                var need_go_down=false;
                                for (i in cc_list!!.indices) {
                                    val single = cc_list.get(i) as FileData
                                    single.local_url =
                                        local_path + getFileNameWithSuffix(single.web_url)
                                    if (FileUtil.isCacheFileExist(single.local_url) == false) {
                                        Log.e(TAG, "onSendEvent: ====>>未下载"+single.web_url )
                                        need_go_down=true
                                        MMKVTool.setBoolean(x.app(),single.local_url,false)
                                    }else{
//                                        Log.e(TAG, "onSendEvent: ====>>已下载"+single.web_url )
                                        MMKVTool.setBoolean(x.app(),single.local_url,true)
                                    }

                                }

                                if(need_go_down){
                                    DownloadListActivity.startActivity(
                                        AppManager.appManager?.currentActivity(),
                                        1
                                    )
                                }
                            }


                        }
                    })

                }

                handler.removeMessages(com.wink_172.model.Constants.EventEnum.检查文件是否有更新.ordinal)
                handler.sendEmptyMessageDelayed(
                    com.wink_172.model.Constants.EventEnum.检查文件是否有更新.ordinal,
                    2000
                )


            }
        }
    }

    override fun onCreate() {
        super.onCreate()

        binder = ServiceBinder()
        handler = MyHandler()

    }

    override fun onBind(intent: Intent?): IBinder? {
        Log.e(TAG, "onCreate: ====>>服务启动")
//        heartRunnable
        handler.removeMessages(com.wink_172.model.Constants.EventEnum.检查文件是否有更新.ordinal)
        handler.sendEmptyMessageDelayed(
            com.wink_172.model.Constants.EventEnum.检查文件是否有更新.ordinal,
            2000
        )
        return binder
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessages(messageBus: CustomEventBus) {

        when (messageBus.type) {

        }
    }


    inner class ServiceBinder : Binder() {

        fun openLog() {
            Log.i(TAG, "============日志")
        }


    }


}